<?php

/* 
 * 内容
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class storyContentController extends AppController
{
    public $class = 'storycontent';
    public function __construct() {
        parent::__construct();
        $this->modelObj = new storyContentModel();
        $this->log_type = '内容管理';
        $this->cookie_key = 'story_content_cookie';
        $this->assign('class', $this->class);
    }
    public function view(){
        $this->get_cookie();
        $this->title = '内容';
        $where = array('is_show' => 1);
        $data = storyTitleModel::load()->getAll(null, $where); 
        $this->assign('titledata', $data);
        $this->_get_data();
        $this->display('content_list');
    }
    

    private function getNextUrl($url) {
        phpQuery::newDocumentFile($url);
//        $prev = pq("link[rel='prev']")->attr('href');
        $next = pq("link[rel='next']")->attr('href');
        if($next == '') {
            return false;//代表当前为最后一页
        }
        return $next;
    }

    public function fx_tianya(){
        $url = trim($_POST['url']);
        $tid = intval($_POST['tid']);
        if($tid <= 0) {
            $this->show_msg('选择主题名称!');
        }
        if($url && $tid > 0) {
            $where = array('tid' => $tid);
            $uid = storyTitleModel::load()->getOne('uid', $where);
            phpQuery::newDocumentFile($url);
            $body = pq('#bd');
            $item = $body->find(".atl-item");
            $data = array();
            $contentObj = storyContentModel::load();
            foreach ($item as $row) {                                
                $reply = pq($row)->find('.atl-reply a:eq(0)');    
                $floor = pq($row)->find('.atl-reply span:eq(0)')->text();
                $authorid = $reply->attr('authorid');   
                if($authorid == $uid) {                    
                    $replyid = intval($reply->attr('replyid'));                    
                    $content = pq($row)->find('.bbs-content')->html();
                    $replytime = $reply->attr('replytime');  
                    //数据库字段     
                    $reg = "/<img(.*)[>|\/>]/is";
                    $content = preg_replace($reg, '', $content);
                    $tmp = array();                    
                    $tmp['tid'] = $tid;
                    $tmp['content'] = addslashes(trim($content));
                    $tmp['replyid'] = $replyid;        
                    $tmp['restime'] = p_str2time($replytime);   
                    $tmp['floor'] = preg_replace("/\D+/", '', $floor);
                    $tmp['ori_url'] = $url;
                    $tmp['in_time'] = p_getTime();                    
                    //非数据库字段
                    $isIn = $contentObj->getReplyId($tid, $replyid); 
                    $isInBool = $isIn !== NULL ? true : false;
                    if(!$isInBool) {//数据库不存在则插入
                        $bool = $contentObj->insert($tmp);
                    }
                    $isIn_str = $isInBool ? '已入库' : ($bool ? '最新入库' : '入库失败');       
                    $header = $floor . '&nbsp;&nbsp;发表时间' . $replytime . '&nbsp;'. $isIn_str;                    
                    $tmp['html'] = $this->in_html($replyid, $header, $content);
                    $data[] = $tmp;
                }
            }
            $next_url = $this->getNextUrl($url);
            $msg = array(
                'next_url' => $next_url,
                'data' => $data,
            );
            exit($this->setReturn(0, $msg));
        }
        exit($this->setReturn(100, 'url 为空'));
    }
    private function in_html($divid, $header, $body){
        $html = <<<eot
<div style="margin: 0;" class="panel panel-default" id="panel-{$divid}">
    <div class="panel-heading" id="paneldiv" data-bind="{$divid}" style="cursor: pointer;">
        <h3 class="panel-title">
             {$header}
             <span class="glyphicon glyphicon-arrow-up" aria-hidden="true"></span>
        </h3>
    </div>
    <div class="panel-body" style="display:none;">
        <p class="text-left">
            {$body}
        </p>
    </div>
</div>
eot;
            return $html;
    }
    public function create(){
        $this->create_token();
        $this->get_cookie();
        $where = array('is_show' => 1);
        $data = storyTitleModel::load()->getAll(null, $where);        
        $this->assign('tdata', $data);
        $this->title = '添加内容';
        $this->display('content_create');
    }
    public function edit(){
        $cid = intval($_GET['id']);
        $this->create_token();
        $this->get_cookie();
        $row = $this->modelObj->get_info($cid);
        $row['tname'] = storyTitleModel::load()->getName($row['tid']);
        $row['ori_url'] = $row['ori_url'] . '#' . $row['floor'];
        $row['floor'] = $row['floor'] == 0 ? '楼主' : $row['floor'] . '楼';        
        $this->assign('row', $row);
        $this->title = '编辑内容';
        $this->display('content_edit');
    }
    public function new_edit(){
        $this->check_token();
        $cid = intval($_POST['cid']);
        $data = array(
            'content' => $_POST['content'],
        );
        $this->set_cookie($data);    
        if($cid < 0) {
            $this->show_msg('编辑CID为空', 'c=' . $this->class . '&m=view');
        }
        $where = array(
            'cid' => $cid,
        );
        $bool = $this->modelObj->update($data, $where);
        if($bool) {
            $this->show_msg('编辑成功', 'c=' .$this->class. '&m=view');
        } else {
            $this->show_msg('编辑失败');
        }
    }
    /**
     * 来源获取数据
     */
    private function _get_data(){
        $page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;  
        $filter['tid'] = isset($_POST['tid']) ? $_POST['tid'] : 0;
        $filter['replyid'] = isset($_POST['replyid']) ? $_POST['replyid'] : 0;
        $filter['content'] = isset($_POST['content']) ? $_POST['content'] : '';
        if($filter['tid'] > 0) {
            $where .= ' and tid = '. $filter['tid'];
        }
        if($filter['replyid'] > 0) {
            $where .= ' and replyid = '. $filter['replyid'];
        }
        if(!empty($filter['content'])) {
            $where .= " and instr(content, '{$filter['content']}')";
        }
        $this->set_cookie($filter);
        $key = $this->modelObj->keyFields;
        $sort_by = isset($_REQUEST['sort_by']) ? $_REQUEST['sort_by'] : $key;
        $sort_order = isset($_REQUEST['sort_order']) ? $_REQUEST['sort_order'] : 'DESC';
        $pageObj = new pageModel('story_content', $where, $this->page_number, 10, $sort_by, $sort_order);        
        $data = $pageObj->pageData($page);
        $list = $data['pageData'];
        if($list) {
            foreach ($list as $key => $row) {
                $data['pageData'][$key]['id'] = $key + 1;
                $tname = storyTitleModel::load()->getName($row['tid']);
                $content = $row['content'];
                $data['pageData'][$key]['tname'] = p_str_len($tname) > 20 ? p_cut_str($tname, 20) . '...' : $tname;
                $data['pageData'][$key]['content'] = p_str_len($content) > 20 ? p_cut_str($content, 20) . '...' : $content; 
                $data['pageData'][$key]['in_time_format'] = publicController::load()->friendlyDate($row['in_time']);       
                $data['pageData'][$key]['restime_format'] = publicController::load()->friendlyDate($row['restime']);
                $data['pageData'][$key]['is_show'] = $row['is_show'] == 1 ? 'glyphicon-ok' : 'glyphicon-remove';  
            }
        }
        $this->assign('sort_' . $sort_by, $data['sort_img']);
        $this->assign('data', $data);
    }
}

